package compress

import (
	"github.com/sirupsen/logrus"
	"os/exec"
	"path"
)

var localImageLocation = "/var/lib/ycontainer/images"

func Pack(containerName, sourcePath string) error {
	tarName := containerName + ".tar"

	if err := exec.Command("tar", "-czf", tarName, "-C", sourcePath, ".").Run(); err != nil {
		logrus.Errorf("commond[tar -czf %s -C %s .]err: %v", tarName, sourcePath, err)
		return err
	}
	return nil
}

func Unpack(imageName, targetPath string) error {
	fullTarName := path.Join(localImageLocation, imageName+".tar")

	if err := exec.Command("tar", "-xvf", fullTarName, "-C", targetPath).Run(); err != nil {
		logrus.Errorf("tar -xvf %v -C %v err: %v", fullTarName, targetPath, err)
		return err
	}
	return nil
}
